package com.luyuan.crm.sale.web;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.faces.model.DataModel;

import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.luyuan.crm.sale.model.Order;
import com.luyuan.crm.sale.model.OrderStatus;
import com.luyuan.crm.sale.service.OrderService;
import com.luyuan.crm.util.ExcelUtil;
import com.ruijie.web.jsf.action.BaseAction;
import com.ruijie.web.jsf.context.TableContext;
import com.ruijie.web.jsf.model.PageDataModel;
import com.ruijie.web.jsf.model.TableOrder;
import com.ruijie.web.jsf.model.TablePage;
import com.ruijie.foundation.query.Page;
import com.ruijie.foundation.query.criteria.CriteriaTemplate;
import com.ruijie.foundation.query.criteria.RestrictionTemplate;

@Component("orderFinanceAction")
@Scope("view")
public class OrderFinanceAction extends BaseAction {

    private Date startDate;

    private Date endDate;

    @Resource(name = "orderService")
    private OrderService orderService;

    /**
     * 已发货的状态的列表
     * 
     * @return
     */
    public DataModel getOrderTableModel() {
        TablePage tablePage = TableContext.getTablePage();
        List<TableOrder> tableOrders = TableContext.getTableOrders();
        List<org.hibernate.criterion.Order> orders = new ArrayList<org.hibernate.criterion.Order>();
        for (TableOrder tableOrder : tableOrders) {
            org.hibernate.criterion.Order order = tableOrder.toOrder();
            if (order != null) {
                orders.add(order);
            }
        }

        CriteriaTemplate c = CriteriaTemplate.forClass(Order.class);
        RestrictionTemplate r = new RestrictionTemplate(false);
        c.add(r.eq("orderStatus", OrderStatus.RECEIVED));

        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -3);
        Date sDate = calendar.getTime();
        Date eDate = new Date();
        if (startDate != null && startDate.after(sDate)) {
            sDate = startDate;
        }
        if (endDate != null && endDate.before(eDate)) {
            eDate = endDate;
        }

        c.add(r.between("createTime", sDate, eDate));

        Page page = orderService.pagedQuery(c, tablePage.getPage(), tablePage.getPageSize(), orders);
        return new PageDataModel(page);
    }

    public void exportExcel() throws IOException {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -3);
        Date sDate = calendar.getTime();
        Date eDate = new Date();
        if (startDate != null && startDate.after(sDate)) {
            sDate = startDate;
        }
        if (endDate != null && endDate.before(eDate)) {
            eDate = endDate;
        }

        List<Order> orders = orderService.findByOrderStatus(OrderStatus.RECEIVED, sDate, eDate);

        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = ExcelUtil.createSheet(wb, "销售记录");

        HSSFRow headRow = sheet.createRow(0);
        Map<Integer, String> headerMap = new HashMap<Integer, String>();
        headerMap.put(0, "订单号");
        headerMap.put(1, "销售员");
        headerMap.put(2, "销售分组");
        headerMap.put(3, "会员");
        headerMap.put(4, "总额");
        headerMap.put(5, "成交额");
        headerMap.put(6, "订单状态");
        headerMap.put(7, "日期");
        ExcelUtil.createHeader(headRow, headerMap);

        int len = orders.size();
        SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日");
        for (int i = 0; i < len; i++) {
            HSSFRow row = ExcelUtil.createRow(sheet, i + 1);

            ExcelUtil.createCell(row, (short) 0).setCellValue(new HSSFRichTextString(orders.get(i).getId()));
            if (orders.get(i).getSaleman() != null) {
                ExcelUtil.createCell(row, (short) 1).setCellValue(
                        new HSSFRichTextString(orders.get(i).getSaleman().getUserName()));
                if (orders.get(i).getSaleman().getPartyGroup() != null) {
                    ExcelUtil.createCell(row, (short) 2).setCellValue(
                            new HSSFRichTextString(orders.get(i).getSaleman().getPartyGroup().getPartyGroupName()));
                }
            }
            ExcelUtil.createCell(row, (short) 3).setCellValue(
                    new HSSFRichTextString(orders.get(i).getCustomer().getName()));
            ExcelUtil.createCell(row, (short) 4).setCellValue(orders.get(i).getOldAmount());
            ExcelUtil.createCell(row, (short) 5).setCellValue(orders.get(i).getAmount());
            ExcelUtil.createCell(row, (short) 6).setCellValue(
                    new HSSFRichTextString(orders.get(i).getOrderStatus().getName()));
            ExcelUtil.createCell(row, (short) 7).setCellValue(
                    new HSSFRichTextString(format.format(orders.get(i).getCreateTime())));

        }

        synchronized (ExcelUtil.class) {
            ExcelUtil.exportExcel(wb, "销售记录.xls");
        }
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }
}
